import type { AxiosRequestConfig } from "axios";
import axios from "axios";
import type { ResponeData } from "@/types/HttpType";
import { message } from "ant-design-vue";
import { useCookies } from "vue3-cookies";
const { cookies } = useCookies();
axios.defaults.timeout = 30000;

axios.defaults.withCredentials = false; // 跨域请求，允许保存cookie

// 添加请求拦截器
axios.interceptors.request.use(
  function (config) {
    config.headers["x-csrf-token"] = cookies.get("token");
    config.headers["Authorization"] = cookies.get("supportToken");
    return config;
  },
  function (error) {
    console.log("error" + error);
    return Promise.reject(error);
  }
);

axios.interceptors.response.use(
  function (response) {
     

    return response;
  },
  function (err) {
    return Promise.reject(err.response.data);
  }
);

export default <T>(options: AxiosRequestConfig): Promise<ResponeData<T>> => {
  return new Promise((resolve, reject) => {
    axios({
      ...options,
    })
      .then((res) => {
        resolve(res.data as ResponeData<T>);
      })
      .catch((err) => {
        reject(err);
      });
  });
};
